<?php
namespace App\Admin\Controllers\ProductStockup;

use App\Admin\Actions\ProductStockup\ImportStockupSkuPackingSpec;
use App\Exporter\ProductStockup\SkuPackingSpecExporter;
use App\Importer\ProductStockup\ImportSkuPackingSpec;
use App\Models\Product\ProductClassify;
use App\Models\ProductStockup\StockupSkuPackingSpec;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;

class StockupSkuPackingSpecController extends AdminController{

    protected $title = '产品装箱规格(新)';

    protected function grid(){

        $grid = new Grid(new StockupSkuPackingSpec());
        $grid->model()->orderBy('ID', 'DESC');
        $grid->column('id','ID')->sortable();
        $grid->column('sku.psku_code','SKU');
        $grid->column('sku.psku_cn','名称');
        $grid->column('sku.classify_id','品类')->display(function ($value){
            return ProductClassify::getTitleById($value);
        });
        $grid->column('num_per_box','每箱发货数量')->editable();

        $grid->fixHeader();
        $grid->disableCreateButton();
        $grid->disableActions();
        $grid->paginate(10);
        $grid->exporter(new SkuPackingSpecExporter()); //导出
        $grid->disableBatchActions();

        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new ImportStockupSkuPackingSpec());

            //$tools->append(new ImportSkuPackingSpec());
        });

        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter){
                $filter->where(function ($query) {
                    $query->whereHas('sku', function ($query) {
                        $query->where('psku_code', 'like', "%{$this->input}%")
                            ->orWhere('psku_cn', 'like', "%{$this->input}%");
                    });
                }, '查询','sku8')->placeholder('模糊查询SKU，名称');
            });
            $filter->column(1/3, function ($filter){
                $filter->equal('sku.classify_id', '品类')->select(ProductClassify::options());
            });
        });

        return $grid;
    }

}
